﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TARLib.Messenger
{
    public static class Management
    {
        public static void SendMessage(string sender, string receiver, string testoMessaggio, DateTime timeStamp_Insert)
        {
            Messaggio messaggioDaSpedire = new Messaggio(null, sender, receiver, testoMessaggio, false, false, timeStamp_Insert);
            messaggioDaSpedire.Send();
        }

        public static List<Messaggio> GetMessaggiDaScaricare(string receiver)
        {
            List<Messaggio> retList = new List<Messaggio>();

            using (SqlConnection connection = new SqlConnection(Database.GetDBConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = "[Messenger].[SP_MessaggiDaRicevere_CL]";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@Receiver", System.Data.SqlDbType.NVarChar, 50) { Value = receiver });

                SqlDataReader dr = null;
                try
                {
                    connection.Open();
                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        retList.Add(new Messaggio(
                            new Guid(dr["MessaggioID"].ToString()),
                            dr["Sender"].ToString(),
                            dr["Receiver"].ToString(),
                            dr["TestoMessaggio"].ToString(),
                            (bool)dr["IsDelivered"],
                            (bool)dr["IsRead"],
                            (DateTime)dr["TimeStamp_Insert"]));
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (dr != null)
                    {
                        dr.Close();
                        dr.Dispose();
                    }
                    connection.Close();
                    cmd.Dispose();
                }
            }

            return retList;
        }

        public static List<Utente> GetStatoUtenti()
        {
            List<Utente> retList = new List<Utente>();

            using (SqlConnection connection = new SqlConnection(Database.GetDBConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = "[Messenger].[SP_StatoUtenti_CL]";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                SqlDataReader dr = null;
                try
                {
                    connection.Open();
                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        retList.Add(new Utente(
                            dr["Username"].ToString(),
                            (bool)dr["IsOnline"]));
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (dr != null)
                    {
                        dr.Close();
                        dr.Dispose();
                    }
                    connection.Close();
                    cmd.Dispose();
                }
            }

            return retList;
        }

        public static List<Messaggio> GetTuttiMessaggi(string sender, string receiver)
        {
            List<Messaggio> retList = new List<Messaggio>();

            using (SqlConnection connection = new SqlConnection(Database.GetDBConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = "[Messenger].[SP_TuttiMessaggi_CL]";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@Sender", System.Data.SqlDbType.NVarChar, 50));
                if (string.IsNullOrEmpty(sender))
                {
                    cmd.Parameters["@Sender"].Value = DBNull.Value;
                }
                else
                {
                    cmd.Parameters["@Sender"].Value = sender;
                }

                cmd.Parameters.Add(new SqlParameter("@Receiver", System.Data.SqlDbType.NVarChar, 50));
                if (string.IsNullOrEmpty(receiver))
                {
                    cmd.Parameters["@Receiver"].Value = DBNull.Value;
                }
                else
                {
                    cmd.Parameters["@Receiver"].Value = receiver;
                }

                SqlDataReader dr = null;
                try
                {
                    connection.Open();
                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        retList.Add(new Messaggio(
                            new Guid(dr["MessaggioID"].ToString()),
                            dr["Sender"].ToString(),
                            dr["Receiver"].ToString(),
                            dr["TestoMessaggio"].ToString(),
                            (bool)dr["IsDelivered"],
                            (bool)dr["IsRead"],
                            (DateTime)dr["TimeStamp_Insert"]));
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (dr != null)
                    {
                        dr.Close();
                        dr.Dispose();
                    }
                    connection.Close();
                    cmd.Dispose();
                }
            }

            return retList;
        }
    }
}
